package com.example.administrator.testpersionn.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.administrator.testpersionn.R;

import java.util.List;
import java.util.Vector;

/**
 * Created by Administrator on 2017/4/28.
 */

public class GridViewCheckAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private int selectorPosition;
    private int lastPosition = -1;   //lastPosition 记录上一次选中的图片位置，-1表示未选中
    private Vector<Boolean> vector = new Vector<Boolean>();

    public GridViewCheckAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = mList;

        for (int i = 0; i < mList.size(); i++) {
            vector.add(false);
        }
    }

    @Override
    public int getCount() {
        return mContext!=null?mList.size():0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


//        //如果当前的position等于传过来点击的position,就去改变他的状态
         ViewHolder holder=null;
        if (convertView == null) {
            holder=new ViewHolder();
         /*   convertView = View.inflate(mContext, R.layout.gridviewitemcheck, null);
            holder.mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.ll);
            holder.textView = (TextView) convertView.findViewById(R.id.tv);*/
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
     /*   Drawable drawable1 = mContext.getResources().getDrawable(R.mipmap.sizexuanzhong_bg);
        Drawable drawabledrow = mContext.getResources().getDrawable(R.drawable.textviewbackgrouddrow);*/
        holder.textView.setText(mList.get(position));
        if (vector.elementAt(position) == true) {
           /* holder.mRelativeLayout.setBackgroundDrawable(drawable1);*/
            holder.textView.setTextColor(Color.parseColor("#306903"));
        } else {
            //其他的恢复原来的状态
        /*    holder.mRelativeLayout.setBackgroundDrawable(drawabledrow);*/
            holder.textView.setTextColor(Color.parseColor("#FFFFC7"));
        }
        return convertView;
    }
    public void changeState(int pos) {
        if(lastPosition != -1)
        vector.setElementAt(false, lastPosition);                   //取消上一次的选中状态
        vector.setElementAt(!vector.elementAt(pos), pos);     //直接取反即可
        lastPosition = pos;                                        //记录本次选中的位置
        notifyDataSetChanged();                                         //通知适配器进行更新
    }
    class ViewHolder{
        RelativeLayout mRelativeLayout;
        TextView textView;
    }

}
